<?php
defined('IN_IA') or exit('Access Denied');
if (!function_exists('m')) {
	function m($name = '')
	{
		static $_modules = array();
		if (isset($_modules[$name])) {
			return $_modules[$name];
		}
		$model = MODULE_ROOT.'/mii/model/' . strtolower($name) . '.php';
		if (!is_file($model)) {
			exit(' Model ' . $name . ' Not Found!');
		}
		require_once $model;
		$class_name = ucfirst($name) . '_MiiModel';
		$_modules[$name] = new $class_name();
		return $_modules[$name];
	}
	if (!function_exists('is_array2')) {
		function is_array2($array)
		{
			if (is_array($array)) {
				foreach ($array as $k => $v) {
					return is_array($v);
				}

				return false;
			}

			return false;
		}
	}

	if (!function_exists('change_medias')) {
		function change_medias($list = array(), $fields = NULL)
		{
			if (empty($list)) {
				return array();
			}

			if (empty($fields)) {
				foreach ($list as &$row) {
					$row = tomedia($row);
				}

				return $list;
			}

			if (!is_array($fields)) {
				$fields = explode(',', $fields);
			}

			if (is_array2($list)) {
				foreach ($list as $key => &$value) {
					foreach ($fields as $field) {
						if (isset($list[$field])) {
							$list[$field] = tomedia($list[$field]);
						}

						if (is_array($value) && isset($value[$field])) {
							$value[$field] = tomedia($value[$field]);
						}
					}
				}

				return $list;
			}

			foreach ($fields as $field) {
				if (isset($list[$field])) {
					$list[$field] = tomedia($list[$field]);
				}
			}

			return $list;
		}
	}	
}
?>